package com.aidong.dynamic.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aidong.dynamic.biz.TrnPraiseBiz;
import com.aidong.dynamic.dao.TrnDynamicCommentDao;
import com.aidong.dynamic.dao.TrnPraiseDao;
import com.aidong.login.dao.MemberInformationDao;

/**
 * 点赞service接口实现
 * @author java
 * @version 1.0    
 *  2015.6.3
 */
@Service("trnPraiseBiz")
public class TrnPraiseBizImpl implements TrnPraiseBiz {

	private TrnPraiseDao trnPraiseDao;
	
	private MemberInformationDao memberInformationDao;
	
	private TrnDynamicCommentDao trnDynamicCommentDao;
	
	@Autowired
	public void setTrnDynamicCommentDao(TrnDynamicCommentDao trnDynamicCommentDao) {
		this.trnDynamicCommentDao = trnDynamicCommentDao;
	}

	@Autowired
	public void setMemberInformationDao(MemberInformationDao memberInformationDao) {
		this.memberInformationDao = memberInformationDao;
	}

	@Autowired
	public void setTrnPraiseDao(TrnPraiseDao trnPraiseDao) {
		this.trnPraiseDao = trnPraiseDao;
	}

	/**
	 * 根据动态ID找出该条动态下的所有点赞人
	 * @param dynamicId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getPraiseByDynamicId(int dynamicId) {
		return trnPraiseDao.getPraiseByDynamicId(dynamicId);
	}
	
	/**
	 * 新增点赞
	 * 借助于运动圈实体类
	 * @param somePraise
	 */
	@Override
	public void addTrnPraise(Map<String, Object> map) {
		trnPraiseDao.addTrnPraise(map);
		// 增加热度值
		Map<String, Object> addUseMap = new HashMap<String, Object>();
		addUseMap.put("idongId", map.get("currentIdongId"));
		memberInformationDao.addMemberPopty(addUseMap);

		Map<String, Object> addPubMap = new HashMap<String, Object>();
		addPubMap.put("idongId", trnDynamicCommentDao.getIdongIdByDymic(Integer.parseInt(map.get("dynamicId").toString())));
		memberInformationDao.addMemberPopty(addPubMap);
	}

	/**
	 * 登陆用户是否已点赞
	 * @param map
	 * @return
	 */
	@Override
	public int getCountPraise(Map<String, Object> map) {
		return trnPraiseDao.getCountPraise(map);
	}

	/**
	 * 修改点赞状态
	 * @param map
	 */
	@Override
	public void editCountPraiseStatus(Map<String, Object> map) {
		trnPraiseDao.editCountPraiseStatus(map);
		int status = Integer.parseInt(map.get("status").toString());
		int idongId = trnDynamicCommentDao.getIdongIdByDymic(Integer.parseInt(map.get("dynamicId").toString()));
		if(status == 1){
			// 增加热度值
			Map<String, Object> addUseMap = new HashMap<String, Object>();
			addUseMap.put("idongId", idongId);
			memberInformationDao.addMemberPopty(addUseMap);
			
			Map<String, Object> addPubMap = new HashMap<String, Object>();
			addPubMap.put("idongId", trnDynamicCommentDao.getIdongIdByDymic(Integer.parseInt(map.get("dynamicId").toString())));
			memberInformationDao.addMemberPopty(addPubMap);
		}else if(status == 0){
			// 减去热度值
			Map<String, Object> delUseMap = new HashMap<String, Object>();
			delUseMap.put("idongId", idongId);
			memberInformationDao.delMemberPopty(delUseMap);
			
			Map<String, Object> delPubMap = new HashMap<String, Object>();
			delPubMap.put("idongId", trnDynamicCommentDao.getIdongIdByDymic(Integer.parseInt(map.get("dynamicId").toString())));
			memberInformationDao.delMemberPopty(delPubMap);
		}
	}

	/**
	 * 根据动态ID找出该条动态下点赞数
	 * @param dynamicId
	 * @return
	 */
	@Override
	public int getPraiseCount(Map<String, Object> map) {
		return trnPraiseDao.getPraiseCount(map);
	}

}
